package xrz.xuanbao.modular.shendaodao;

import java.util.ArrayList;

import org.xutils.x;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.common.util.LogUtil;

import xrz.cuanbao.R;
import xrz.cuanbao.activity.Act_BigImage;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.xrz.sxm.base.AdapterBase;
import com.xrz.sxm.base.utils.ScreenUtil;

public class Adapter_Pics extends AdapterBase<String>{

	private LayoutParams params;
	private int itemWidth;
	public Adapter_Pics(Context ctx) {
		super(ctx);
	}

	@Override
	protected View getExView(final int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = new ImageView(m_Context);
		}
		final ImageView imageView = (ImageView) convertView;
		imageView.setScaleType(ScaleType.CENTER_CROP);
		x.image().bind(imageView, getList().get(position),new CommonCallback<Drawable>() {
			
			@Override
			public void onSuccess(Drawable result) {
				// TODO Auto-generated method stub
//				int width = imageView.getWidth();
//				if(width != 0){
//					itemWidth = width;
//				}
//				if(itemWidth == 0)               
					itemWidth = (int) (ScreenUtil.getInstant().getDisWidth(m_Context)/3 - 60);
					LogUtil.e("itemWidth="+itemWidth);
				if(itemWidth != 0 && params != null){
					imageView.setLayoutParams(params);
				}else{
					params = new LayoutParams(LayoutParams.MATCH_PARENT, itemWidth);
					imageView.setLayoutParams(params);
				}
				notifyDataSetChanged();
			}
			
			@Override
			public void onFinished() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onCancelled(CancelledException cex) {
				// TODO Auto-generated method stub
				
			}
		});
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(m_Context, Act_BigImage.class);
				ArrayList<String> list = new ArrayList<String>();
				for(String s:getList()){
					list.add(s);
				}
				intent.putStringArrayListExtra("paths", list);
				intent.putExtra("pos", position);
				m_Context.startActivity(intent);
			}
		});
		return convertView;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}


}
